﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for BillBUS
/// </summary>
public class BillBUS
{
    BillDAL billDAL = new BillDAL();
    BillDetailsDAL billDetailsDAL = new BillDetailsDAL();
    public BillBUS()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataTable returnAllBill()
    {
        return billDAL.getAllBill();
    }

    public DataTable returnBillByID(int id)
    {
        return billDAL.getBillByID(id);
    }
    public bool updateBillStatus(int Bi_ID, string Status)
    {
        return billDAL.updateBillStatus(Bi_ID, Status);
    }

    public DataTable returnBillDoneStatus(string dateCompleted)
    {
        return billDAL.getBillDoneStatus(dateCompleted);
    }
    public DataTable returnBillNotStatus(string dateCompleted)
    {
        return billDAL.getBillNotStatus(dateCompleted);
    }

    public DataTable returnBillByDateAndStatus(string dateCompleted)
    {
        return billDAL.getBillByDateAndStatus(dateCompleted);
    }

    public List<BouquetNumber> CountNumberBouquet(string dateCompleted)
    {

        List<BouquetNumber> result = new List<BouquetNumber>();
        DataTable tbBill = new DataTable();
        DataTable tbBillDetails = new DataTable();

        tbBill = returnBillByDateAndStatus(dateCompleted);
        for (int i = 0; i < tbBill.Rows.Count; i++)
        {
            tbBillDetails = billDetailsDAL.getBillDetailsById(Int32.Parse(tbBill.Rows[i]["Bi_ID"].ToString()));
            for (int j = 0; j < tbBillDetails.Rows.Count; j++)
            {
                BouquetNumber bNumber = new BouquetNumber(tbBillDetails.Rows[j]["B_ID"].ToString(), Int32.Parse(tbBillDetails.Rows[j]["BiD_Number"].ToString()), float.Parse(tbBillDetails.Rows[j]["BiD_Amount"].ToString()));
                result.Add(bNumber);
            }
        }

        for (int i = 0; i < result.Count - 1; i++)
        {
            for (int j = i + 1; j < result.Count; j++)
            {
                if (result[i].Id.Equals(result[j].Id))
                {
                    result[i].Number = result[i].Number + result[j].Number;
                    result[i].Amount = result[i].Amount + result[j].Amount;
                    result.RemoveAt(j);
                }
            }
        }
        return result;
    }

    public bool insertBill(float totalAmount, string address, string date, string dateC, string memID, string mess, string status)
    {
        return billDAL.insertBill(totalAmount, address, date, dateC, memID, mess, status);
    }

    public DataTable returnBillByManyFiels(float totalAmount, string date, string dateC, string address, string memID)
    {
        return billDAL.getBillByManyFiels(totalAmount, date, dateC, address, memID);
    }

    public List<Bill> Statistic(DateTime fromDate, DateTime toDate, string status)
    {
        DataTable statistic = new DataTable();
        List<Bill> lstTemp = new List<Bill>();
        List<Bill> lstBill = new List<Bill>();
        statistic = returnAllBill();
        for (int i = 0; i < statistic.Rows.Count; i++)
        {
            Bill bill = new Bill(Int32.Parse(statistic.Rows[i]["Bi_ID"].ToString()), float.Parse(statistic.Rows[i]["Bi_TotalAmount"].ToString()),
                statistic.Rows[i]["Bi_Address"].ToString(), DateTime.Parse(statistic.Rows[i]["Bi_Date"].ToString()),
                DateTime.Parse(statistic.Rows[i]["Bi_DateCompleted"].ToString()), statistic.Rows[i]["M_ID"].ToString(),
                statistic.Rows[i]["Bi_Messages"].ToString(), statistic.Rows[i]["Bi_Status"].ToString());
            lstTemp.Add(bill);
        }
        foreach (Bill item in lstTemp)
        {
            if (item.Status.Equals(status))
            {
                if (item.Date >= fromDate && item.Date <= toDate.AddDays(1))
                {
                    lstBill.Add(item);
                }
            }
        }
        return lstBill;
    }
}